package cn.com.eado.crm.m.db

import androidx.room.Entity
import androidx.room.PrimaryKey
import androidx.room.TypeConverter
import androidx.room.TypeConverters
import com.google.gson.Gson
import com.google.gson.reflect.TypeToken

/**
 * 企业和设计院合并一个类
 */
@Entity(tableName = "company")
@TypeConverters(Company.Converters::class)
data class Company(
    @PrimaryKey(autoGenerate = true)
    val id: Int,
    var userId: Int,
    //
    val rows: List<Row>,
    val total: Int
){
    data class Row(
        //企业、设计院
        val C_Address: String,
        val C_OwnerShip: String,
        val CompanyID: String,
        val CompanyName: String,
        val CompanyName_en: String,
        val CreateDate: String,
        val EntityKey: String,
        val EntityState: String,
        val GroupID: String,
        val GroupName: String,
        val IndustryID: String,
        val IndustryName: String,
        val IsLarge: String,
        val LastTrackData: String,
        val RecordCount: String,
        val Remark: String,
        val TrackCount: String,
        val UpdateDate: String,
        val UserID: String,
        val UserName: String,
        //设计院特有字段
//        val Address: String,
//        val Designers: String,
//        val InstituteID: String,
//        val InstituteName: String,
//        val InstituteName_en: String,
//        val LastTrackDate: String,
        //自行添加3个字段
        val city: String?,
        val industryType: String?,
        var selected: Boolean
    )

    class Converters{
        @TypeConverter
        fun stringToObject(value: String): List<Row> {
            val listType = object : TypeToken<List<Row>>() {
            }.type
            return Gson().fromJson(value, listType)
        }

        @TypeConverter
        fun objectToString(list: List<Row>): String {
            val gson = Gson()
            return gson.toJson(list)
        }
    }
}